package com.tourManagement.controller;

import com.tourManagement.entity.Notice;
import com.tourManagement.entity.RT;
import com.tourManagement.entity.行程信息;
import com.tourManagement.service.NoticeService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;


@RestController
@AllArgsConstructor
@RequestMapping("/notice")
public class NoticeController {
    private NoticeService noticeService;

    @GetMapping("/test")
    public String test() {
        return "111";
    }

    /*
    * 发布通知
    */
    @PostMapping("/save")
    public RT insert(@RequestBody Notice notice) throws SQLException{
        LocalDateTime now = LocalDateTime.now();
        notice.setDate(now);

        return RT.success(noticeService.insert(notice));
    }

    /*
    * 根据通知id删除通知
    * */
    @PostMapping("/delete")
    public RT delete(int id) throws SQLException {
        if (id==0) {
            return RT.error("id不能为空");
        }
        return RT.success(noticeService.delete(id));
    }

    /*
    *更改通知内容
    * */
    @PostMapping("/updateContent")
    public RT updateContent(@RequestBody Notice notice) throws SQLException {

        LocalDateTime now = LocalDateTime.now();
        notice.setDate(now);
        if (notice.getId()==0) {
            return RT.error("id不能为空");
        }
        return RT.success(noticeService.updateContent(notice));
    }

    /*
     *更改通知标题
     * */
    @PostMapping("/updateTitle")
    public RT updateTitle(@RequestBody Notice notice) throws SQLException {

        LocalDateTime now = LocalDateTime.now();
        notice.setDate(now);
        if (notice.getId()==0) {
            return RT.error("id不能为空");
        }
        return RT.success(noticeService.updateTitle(notice));
    }

    /*
    * 模糊查询通知内容
    * */
    @GetMapping("/getByContent")
    public RT selectByRoute(String content) throws SQLException {
        return RT.success(noticeService.selectByContent(content));
    }

    /*
    * 获取所有通知
    * */
    @GetMapping("/all")
    public RT select() throws SQLException{
        return RT.success(noticeService.select());
    }

}
